//
//  LZRestaurantAuthSelectCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/3.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZRestaurantAuthSelectCell.h"
#import "UIView+Rotation.h"
#import "LZRestaurantAuthModel.h"

@interface LZRestaurantAuthSelectCell()
@property (weak, nonatomic) IBOutlet UILabel *cellNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *showResultLabelOne;
@property (weak, nonatomic) IBOutlet UILabel *showResultLabelTwo;
@property (weak, nonatomic) IBOutlet UIImageView *rightArrawImg;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cellNameLabelBottomToSuperViewCST;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cellNameLabeBottomToLabelTwoCST;
@property (strong, nonatomic) NSIndexPath *indexPath;
@end

@implementation LZRestaurantAuthSelectCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.showResultLabelTwo.preferredMaxLayoutWidth = SCREEN_WIDTH - 30;
    [self.rightArrawImg setLanguageRotaion];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAction)];
    [self addGestureRecognizer:tap];
}

#pragma mark -
#pragma mark load  data
- (void)loadSelectCellWithIndexPath:(NSIndexPath *)indexPath andModel:(LZRestaurantAuthModel *)authModel
{
    self.cellNameLabel.text = @"";
    self.showResultLabelTwo.text = @"";
    self.showResultLabelOne.text = @"";
    self.indexPath = indexPath;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 1:{//菜系
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_cuisine", nil);
                BOOL result = [self contentCanShowInFirtLineWithContentStr:authModel.cookingStyleName];
                if (result) {
                    self.showResultLabelOne.text = authModel.cookingStyleName;
                    self.showResultLabelTwo.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 999;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 250;
                }else{
                    self.showResultLabelTwo.text = authModel.cookingStyleName;
                    self.showResultLabelOne.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 250;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 999;
                }
            }
                break;
            case 3:{ //地图定位地址
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_map_location", nil);
                BOOL result = [self contentCanShowInFirtLineWithContentStr:authModel.mapLocationAddress];
                if (result) {
                    self.showResultLabelOne.text = authModel.mapLocationAddress;
                    self.showResultLabelTwo.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 999;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 250;
                }else{
                    self.showResultLabelTwo.text = authModel.mapLocationAddress;
                    self.showResultLabelOne.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 250;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 999;

                }
            }
                break;
            case 4:{//城市选择
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_city_location", nil);
                BOOL result = [self contentCanShowInFirtLineWithContentStr:authModel.cityName];
                if (result) {
                    self.showResultLabelOne.text = authModel.cityName;
                    self.showResultLabelTwo.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 999;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 250;
                }else{
                    self.showResultLabelTwo.text = authModel.cityName;
                    self.showResultLabelOne.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 250;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 999;
                    
                }
            }
                break;
            case 5:{//热门商区
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_area", nil);
                BOOL result = [self contentCanShowInFirtLineWithContentStr:authModel.areaName];
                if (result) {
                    self.showResultLabelOne.text = authModel.areaName;
                    self.showResultLabelTwo.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 999;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 250;
                }else{
                    self.showResultLabelTwo.text = authModel.areaName;
                    self.showResultLabelOne.text = @"";
                    self.cellNameLabelBottomToSuperViewCST.priority = 250;
                    self.cellNameLabeBottomToLabelTwoCST.priority = 999;

                }
            }
                break;
                
            default:
                break;
        }
    }else if (indexPath.section == 1){
        self.cellNameLabel.text = LZLocalizedString(@"res_auth_pay_account", nil);
        self.showResultLabelOne.text = @"";
        self.cellNameLabelBottomToSuperViewCST.priority = 250;
        self.cellNameLabeBottomToLabelTwoCST.priority = 999;
        if ([authModel.paymentMethod isEqualToString:@"1"]) {
            self.showResultLabelTwo.text = authModel.paypalNumber;
        }else if ([authModel.paymentMethod isEqualToString:@"2"]){
            self.showResultLabelTwo.text = [NSString stringWithFormat:@"%@ %@",authModel.bankName,authModel.bankNumber];
        }else{
            self.showResultLabelTwo.text = @"";
            self.cellNameLabelBottomToSuperViewCST.priority = 999;
            self.cellNameLabeBottomToLabelTwoCST.priority = 250;
        }
    }
}

/**
 获取cell高度
 
 @param text 内容值
 @param cellNameText title
 @return <#return value description#>
 */
+ (CGFloat)getCellHeightWithContext:(NSString *)text cellNameText:(NSString *)cellNameText
{
    CGFloat nameW = [cellNameText boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.width;
    
    CGFloat textW = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.width;
    CGFloat canSHowWidth = SCREEN_WIDTH - nameW - 20 - 35;
    if (canSHowWidth > textW) {
        return 58;
    }else{
      CGFloat textH = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
        
        return 58 + textH + 25;
    }
}

/**
 这个方法只针对账户选项的cell.因为这里无论第一行是否放得下,都要放在第二行.上面的获取高度方法不使用
 
 @param str 内容
 @return <#return value description#>
 */
+ (CGFloat)getCellHeightForAccountCellWithContent:(NSString *)str
{
    if (str.length == 0) {
        return 58;
    }
    CGFloat textH = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
    
    return 58 + textH + 25;
}


/**
 是否允许编辑

 @param enable <#enable description#>
 */
- (void)changeCellEnabled:(BOOL)enable
{
    self.userInteractionEnabled = enable;
}

#pragma mark -
#pragma mark priate method

/**
 检测展示内容是否可以在第一行,也就是标题后面能够展示

 @param str 将要展示的内容
 @return YES,说明可以在后面直接展示, NO说明第一行后面空间不足,需要在下面展示
 */
- (BOOL)contentCanShowInFirtLineWithContentStr:(NSString *)str
{
    CGFloat contentW = [self getContentWidth:str];
    CGFloat canShowW = [self getTotleCanShowContentWidth];
    
    return canShowW > contentW;
}

/**
 获取展示内容的宽度

 @param str 将要展示的内容
 @return <#return value description#>
 */
- (CGFloat)getContentWidth:(NSString *)str
{
    CGFloat w = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.width;
    return w;
}


/**
 获取标题后面可展示的宽度

 @return <#return value description#>
 */
- (CGFloat)getTotleCanShowContentWidth
{
    CGFloat labelW = [self.cellNameLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.width;
    return SCREEN_WIDTH - labelW - 10 - 10 - 10 - 10 - 15;
}


#pragma mark -
#pragma mark event
- (void)selectAction
{
    if (self.restaurantAuthSelectCellDelegate && [self.restaurantAuthSelectCellDelegate respondsToSelector:@selector(clickSelectCellWithIndexPath:)]) {
        [self.restaurantAuthSelectCellDelegate clickSelectCellWithIndexPath:self.indexPath];
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
